import 'package:eshi/app/shi/data/Config.dart';
import 'package:eshi/app/shi/data/HistoryPoint.dart';

/// 一个历史的搜索结果 
class HistorySearchResult{
  HistoryPoint point; //point节点
  HistoryPointSearchResult pointResult;
  

  HistorySearchResult(this.point, this.pointResult);
  
  static List<HistorySearchResult> searchResult = [];

  static void add(HistoryPoint point, HistoryPointSearchResult pointResult) {
    searchResult.add(HistorySearchResult(point,pointResult));
  }

  static HistorySearchResult search(String key, int index) {
    final result = searchResult[index];
    return result;
  }
}

/// 一个历史节点的搜索结果 
class HistoryPointSearchResult{
    int tabIndex;  //第几个tab
    int pageIndex; //当前页面中第几个搜索 
    RegExpMatch matchInfo;  //搜索的信息 
    get start => matchInfo.start; //开始时位置 
    
    HistoryPointSearchResult(this.tabIndex, this.pageIndex, this.matchInfo);

    static List<HistoryPointSearchResult> all=[];
    
    static void add(HistoryPointSearchResult searchResult) => all.add(searchResult);
  
    static HistoryPointSearchResult get(int pos) => all[pos];
    
    static void clear() => all=[];
}
/// 一次搜索的搜索的总结果
class SearchResult{
  final int count;
  final String key;
  const SearchResult(this.count, this.key);
}

/// 搜索历史
class SearchResultHistory{
  static String cacheKey="searchHistory";
  static List<String>? data =<String>[];
  static void add(String query) {
    //去掉重复 
    final haveIndex = data!.indexOf(query);
    if(haveIndex != -1){
      data!.removeAt(haveIndex);
    }
    //前方插入
    data!.insert(0,query);
    //限制50个
    if(data!.length>50) data!.removeLast();
    //保存
    Config.getInstance()?.setStringList(cacheKey, data!);
  }

  static List<String> getData() {
    //应用启动只从存储获取一次 通过data做缓存 
    if(data!.isNotEmpty) return data!;
    //获取数据 并保证是数组 
    data = Config.getInstance()?.getStringList(cacheKey);
    data ??= <String>[];
    return data!;
  }

}